<?php
namespace Admin\Controller;
use Common\Controller\AuthController;
class UserController extends AuthController{
	//Auth认证管理
	public function index1(){
		//获取用户信息
		$user=D("user")->relation(true)->field("password",true)->select();
		//relation(true)关联，field("password",true)字段排除，获取除password外其他数据
		$this->user=$user;
		//获取用户组信息
		$group=M("auth_group")->select();
		$obj=M("auth_rule");
		foreach($group as $k=>$v){
			$map['id'] = array('in',$group[$k]['rules']);
			$group[$k]['group']=$obj->where($map)->select();
		}
		$this->group=$group;
		//获取rule规则
		$this->rule=M("auth_rule")->select();

	    echo json_encode($this->user);

	}
	public function index(){
		$this->display();
	}
/**
 * [info 显示当前用户信息]
 * @return [type] [description]
 */
public function info(){
	$user=D("user");
	$id=session('uid');
	$map['id']=$id;
	$data=$user->where($map)->find();
	$this->assign($data);
	$this->display();
	// var_dump($data);
}
/**
 * [avatar 上传avatar头像]
 * @return [type] [description]
 */
public function avatar(){
	$avatar=$_POST['avatar'];


$base64_body = substr(strstr($avatar,','),1);
$data= base64_decode($base64_body );
$filename=session('uname').rand();
file_put_contents('./Public/upload/avatar/'.$filename.'.jpg',$data);
// 将地址写入数据库
$user=M("user");
$map['id']=session('uid');
$datas['avatar']='upload/avatar/'.$filename.'.jpg';
$re=$user->where($map)->save($datas);



if($re){
	$dataa['status']=1;
	$dataa['msg']='上传头像成功';
}else{
	$dataa['status']=0;
	$dataa['msg']='上传头像失败';
}
echo json_encode($dataa);
}

/**
 * [upload_sign 上传签名]
 * @return [type] [description]
 */
public function upload_sign(){
$upload = new \Think\Upload();// 实例化上传类
  $upload->maxSize   =     3145728 ;// 设置附件上传大小
   $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型

    	$upload->rootPath      =  './Public/'; //保存根路径
				$upload->savePath =  '/upload/sign/';// 设置附件上传目录

    $info   =   $upload->upload();
     if(!$info) {// 上传错误提示错误信息
 
     	$dataa['status']=0;
		$dataa['msg']=$upload->getError();
     	  }else{// 上传成功

     	  	$user=M("user");
$map['id']=session('uid');
$datas['sign']=$info['sign']['savepath'].$info['sign']['savename'];
$re=$user->where($map)->save($datas);


     	  	$dataa['status']=1;
			$dataa['msg']='上传签名成功';
     	  }
     	  echo json_encode($dataa);
     	}
	public function userlist(){
	//获取用户信息
		$user=D("user");
		$pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
	    $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	    // 排除password
		$userlist=$user->relation(true)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->field("password",true)->order('id asc')->select();
	    echo json_encode($userlist);	
	}
public function authgrouplist(){
	$auth_group = M("auth_group");
	$data=$auth_group->order('id asc')->select();
	echo json_encode($data);
	}

	//添加后台用户及表单处理
	public function adduser(){
		if(IS_POST){
			$data=array(
					'username'=>I('username','','trim'),
					'nickname'=>I('nickname','','trim'),
					// 'group_id'=>I('group_id','','trim'),
					'email'=>I('email','','trim'),
					'password'=>I('password','','md5'),
					'repassword'=>I('repassword','','md5'),
					'status'=>I('status',0,'intval'),
					
					'creattime'=>date('y-m-d h:i:s',$_SERVER['REQUEST_TIME']),
					'login_num'=>0
			);

			
			$rules = array(
			   array('username','require','用户名必须！'),
			   array('username','','帐号名称已经存在！',0,'unique',1), 
          	   array('username','3,12','用户名不符合要求(3到12个字符之间)。',0,'length'),
           	   array('group_id','require','用户组必须！'),
			   array('email','email','邮箱格式不正确'),
			   array('group_id',array(1,2,3),'值的范围不正确！',0,'in'), 
			   array('repassword','password','确认密码不正确',0,'confirm'), 
	/* 		   array('password','/^[a-z]\w{6,30}$/i','密码不符合要求！'), */
          );	

			
			
			$User = M("User"); // 实例化User对象
			if (!$User->validate($rules)->create($data)){

				// 如果创建失败 表示验证没有通过 输出错误提示信息
			
				exit($User->getError());
			}else{
				$result = $User->add(); // 写入数据到数据库
				if($result){
					// 如果主键是自动增长型 成功后返回值就是最新插入的值
					

					
					// $data1['group_id']=implode(',', I('classify'));
					$group_ids=I('classify');
					foreach ($group_ids as $key => $value) {
						$data1['uid']=$result;
						$data1['group_id']=$group_ids[$key];
						$result2=M('auth_group_access')->add($data1);
					}
					// $result1=M('group_access');
				}
				$this->show("添加成功...");
				// 验证通过 可以进行其他数据操作
			}
				
			
		}else{
			$this->group=M("auth_group")->field("id,title")->select();
			$this->display();
		}
	
	}

		//删除用户
	public function deleteUser(){
		if($_POST['id']){
			$map['id']=$_POST['id'];
			$mapa['uid']=$_POST['id'];
			$result=M('user')->where($map)->delete();
			$result1=M('auth_group_access')->where($mapa)->delete();
			if($result){
				$data['status']=1;
            	$data['data']="删除成功";
            	echo json_encode($data);
			}else{
				$data['status']=0;
            	$data['data']="删除失败";
            	echo json_encode($data);
			}

		}else{
			$data['status']=0;
            $data['data']="删除失败";
            echo json_encode($data);
		}
	}




	//修改用户
	public function modifyUser(){
		if($_POST['id']){
			$map['id']=$_POST['id'];
			// $data['username']=I("username","","trim");
			$data['nickname']=I("nickname","","trim");
			$data['email']=I("email","","trim");
 			$data['status']=I("status",0,"intval");
 			$classify=I("classify");
 			if(trim($_POST['password'])){
				if(md5($_POST['password'])!=md5($_POST['repassword'])){
					$data['status']=0;
					$data['data']="两次密码输入不一致...";
					echo json_encode($data);
				}else{
					$data['password']=I("password","","md5");
				}
			}

			 $result=M('user')->where($map)->save($data);
			 if(count($classify)>0){
			 	$mapb['uid']=$_POST['id'];
			 	$resultclear=M('auth_group_access')->where($mapb)->delete(); 
			 	foreach ($classify as $key => $value) {
		 			$data1['uid']=$_POST['id'];
					$data1['group_id']=$classify[$key];
					$result2=M('auth_group_access')->add($data1);
			 	}
			 }
			 if($result){
			 	$data['status']=1;
				$data['data']="修改用户成功...";
				echo json_encode($data);
			 }else{
                 $data['status']=0;
                 $data['data']="修改用户失败...";
                 echo json_encode($data);
             }
			 
		}
	}
	public function modifyUser1(){
		if(IS_POST){
		
			 $id=I("id",0,"intval");
			if(!$id){
			
 				return false;
			}
			$data['username']=I("username","","trim");
			$data['email']=I("email","","trim");
 			$data['status']=I("status",0,"intval");
			$data['group_id']=I("group_id",0,"intval");
			$tmp=0;
			if(isset($_POST['role_id'])){
				M("auth_group_access")->where(array("uid"=>$id))->delete();
				foreach ($_POST['role_id'] as $key => $value) {
					if(M("auth_group_access")->add(array("uid"=>$id,"group_id"=>$_POST['role_id'][$key]))){
						$tmp=1;
					}
				}
			}
			if(trim($_POST['password'])){
				if(md5($_POST['password'])!=md5($_POST['repassword'])){
// 					$this->error("两次密码输入不一致...");
					$this->show("两次密码输入不一致...");
				}else{
					$data['password']=I("password","","md5");
				}
			}
			if(M("user")->where(array("id"=>$id))->save($data)){
// 				$this->success("修改成功...",U("Auth/index"));
				$this->show("修改成功...");
			}else{
				if($tmp){
// 					$this->success("修改成功...",U("Auth/index"));
					$this->show("修改成功...");
				}else{
// 					$this->error("修改失败...");
					$this->show("修改失败...");
				}
			} 
		}elseif(IS_GET){
			if(!isset($_GET['id'])){
				return false;
			}
			$id=I("id",0,"intval");
			if(!$id){
				return false;
			}
			$this->user=M("user")->where(array('id'=>$id))->field("password",true)->find();
			$user_group=M("auth_group_access")->where(array("uid"=>$id))->select();
			$group=M("auth_group")->select();
			foreach($user_group as $key => $value){
				$user_group[$key]['group_name']=M("auth_group")->where(array("id"=>$user_group[$key]['group_id']))->getField("title");
			}
			$this->user_group=$user_group;
			$this->group=$group;
			$this->display();
		}
}

}

?>